home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / lang / Class.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  58.2 KB  |  1,353 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)Class.java    1.97 98/10/30
  3.  *
  4.  * Copyright 1994-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.lang;
  16.  
  17. import java.lang.reflect.Member;
  18. import java.lang.reflect.Field;
  19. import java.lang.reflect.Method;
  20. import java.lang.reflect.Constructor;
  21. import java.lang.reflect.Modifier;
  22. import java.io.InputStream;
  23. import java.io.ObjectStreamClass;
  24. import java.io.ObjectStreamField;
  25.  
  26.  
  27. /**
  28.  * Instances of the class <code>Class</code> represent classes and interfaces
  29.  * in a running Java application.  Every array also belongs to a class that is
  30.  * reflected as a <code>Class</code> object that is shared by all arrays with
  31.  * the same element type and number of dimensions.  The primitive Java types
  32.  * (<code>boolean</code>, <code>byte</code>, <code>char</code>,
  33.  * <code>short</code>, <code>int</code>, <code>long</code>,
  34.  * <code>float</code>, and <code>double</code>), and the keyword
  35.  * <code>void</code> are also represented as <code>Class</code> objects.
  36.  *
  37.  * <p> <code>Class</code> has no public constructor. Instead <code>Class</code>
  38.  * objects are constructed automatically by the Java Virtual Machine as classes
  39.  * are loaded and by calls to the <code>defineClass</code> method in the class
  40.  * loader.
  41.  *
  42.  * <p> The following example uses a <code>Class</code> object to print the
  43.  * class name of an object:
  44.  *
  45.  * <p> <blockquote><pre>
  46.  *     void printClassName(Object obj) {
  47.  *         System.out.println("The class of " + obj +
  48.  *                            " is " + obj.getClass().getName());
  49.  *     }
  50.  * </pre></blockquote>
  51.  *
  52.  * @author  unascribed
  53.  * @version 1.97, 10/30/98
  54.  * @see     java.lang.ClassLoader#defineClass(byte[], int, int)
  55.  * @since   JDK1.0
  56.  */
  57. public final
  58. class Class implements java.io.Serializable {
  59.  
  60.  
  61.     private static native void registerNatives();
  62.     static {
  63.         registerNatives();
  64.     }
  65.  
  66.  
  67.     /*
  68.      * Constructor. Only the Java Virtual Machine creates Class
  69.      * objects.
  70.      */
  71.     private Class() {}
  72.  
  73.  
  74.     /**
  75.      * Converts the object to a string. The string representation is the
  76.      * string "class" or "interface", followed by a space, and then by the
  77.      * fully qualified name of the class in the format returned by
  78.      * <code>getName</code>.  If this <code>Class</code> object represents a
  79.      * primitive type, this method returns the name of the primitive type.  If
  80.      * this <code>Class</code> object represents void this method returns
  81.      * "void".
  82.      *
  83.      * @return a string representation of this class object.
  84.      */
  85.     public String toString() {
  86.         return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
  87.             + getName();
  88.     }
  89.  
  90.  
  91.     /**
  92.      * Returns the <code>Class</code> object associated with the class or
  93.      * interface with the given string name.  Invoking this method is
  94.      * equivalent to:
  95.      *
  96.      * <blockquote><pre>
  97.      *  Class.forName(className, true, currentLoader)
  98.      * </pre></blockquote>
  99.      *
  100.      * where <code>currentLoader</code> denotes the defining class loader of
  101.      * the current class.
  102.      *
  103.      * <p> For example, the following code fragment returns the
  104.      * runtime <code>Class</code> descriptor for the class named
  105.      * <code>java.lang.Thread</code>:
  106.      *
  107.      * <blockquote><pre>
  108.      *   Class t = Class.forName("java.lang.Thread")
  109.      * </pre></blockquote>
  110.      * <p>
  111.      * A call to <tt>forName("X")</tt> causes the class named 
  112.      * <tt>X</tt> to be initialized.
  113.      *
  114.      * @param      className   the fully qualified name of the desired class.
  115.      * @return     the <code>Class</code> object for the class with the
  116.      *             specified name.
  117.      * @exception LinkageError if the linkage fails
  118.      * @exception ExceptionInInitializerError if the initialization provoked
  119.      *            by this method fails
  120.      * @exception ClassNotFoundException if the class cannot be located
  121.      */
  122.     public static Class forName(String className) 
  123.                 throws ClassNotFoundException {
  124.         return forName0(className, true, ClassLoader.getCallerClassLoader());
  125.     }
  126.  
  127.  
  128.     /**
  129.      * Returns the <code>Class</code> object associated with the class or
  130.      * interface with the given string name, using the given class loader.
  131.      * Given the fully qualified name for a class or interface (in the same
  132.      * format returned by <code>getName</code>) this method attempts to
  133.      * locate, load, and link the class or interface.  The specified class
  134.      * loader is used to load the class or interface.  If the parameter
  135.      * <code>loader</code> is null, the class is loaded through the bootstrap
  136.      * class loader.  The class is initialized only if the
  137.      * <code>initialize</code> parameter is <code>true</code> and if it has
  138.      * not been initialized earlier.
  139.      *
  140.      * <p> If <code>name</code> denotes a primitive type or void, an attempt
  141.      * will be made to locate a user-defined class in the unnamed package whose
  142.      * name is <code>name</code>. Therefore, this method cannot be used to
  143.      * obtain any of the <code>Class</code> objects representing primitive
  144.      * types or void.
  145.      *
  146.      * <p> If <code>name</code> denotes an array class, the component type of
  147.      * the array class is loaded but not initialized.
  148.      *
  149.      * <p> For example, in an instance method the expression:
  150.      *
  151.      * <blockquote><pre>
  152.      *  Class.forName("Foo")
  153.      * </pre></blockquote>
  154.      *
  155.      * is equivalent to:
  156.      *
  157.      * <blockquote><pre>
  158.      *  Class.forName("Foo", true, this.getClass().getClassLoader())
  159.      * </pre></blockquote>
  160.      *
  161.      * Note that this method throws errors related to loading, linking or
  162.      * initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The
  163.      * Java Language Specification</em>.
  164.      *
  165.      * <p> If the <code>loader</code> is <code>null</code>, and a security
  166.      * manager is present, and the caller's class loader is not null, then this
  167.      * method calls the security manager's <code>checkPermission</code> method
  168.      * with a <code>RuntimePermission("getClassLoader")</code> permission to
  169.      * ensure it's ok to access the bootstrap class loader.
  170.      *
  171.      * @param name       fully qualified name of the desired class
  172.      * @param initialize whether the class must be initialized
  173.      * @param loader     class loader from which the class must be loaded
  174.      * @return           class object representing the desired class
  175.      * 
  176.      * @exception LinkageError if the linkage fails
  177.      * @exception ExceptionInInitializerError if the initialization provoked
  178.      *            by this method fails
  179.      * @exception ClassNotFoundException if the class cannot be located by
  180.      *            the specified class loader
  181.      *
  182.      * @see       java.lang.Class#forName(String) 
  183.      * @see       java.lang.ClassLoader
  184.      * @since       JDK1.2
  185.      */
  186.     public static Class forName(String name, boolean initialize,
  187.                 ClassLoader loader)
  188.         throws ClassNotFoundException
  189.     {
  190.     if (loader == null) {
  191.         SecurityManager sm = System.getSecurityManager();
  192.         if (sm != null) {
  193.         ClassLoader ccl = ClassLoader.getCallerClassLoader();
  194.         if (ccl != null) {
  195.             sm.checkPermission(ClassLoader.getGetClassLoaderPerm());
  196.         }
  197.         }
  198.     }
  199.     return forName0(name, initialize, loader);
  200.     }
  201.  
  202.     /** Called after security checks have been made. */
  203.     private static native Class forName0(String name, boolean initialize,
  204.                      ClassLoader loader)
  205.     throws ClassNotFoundException;
  206.  
  207.     /**
  208.      * Creates a new instance of the class represented by this <tt>Class</tt>
  209.      * object.  The class is instantiatied as if by a <code>new</code>
  210.      * expression with an empty argument list.  The class is initialized if it
  211.      * has not already been initialized.
  212.      *
  213.      * <p>If there is a security manager, this method first calls the security
  214.      * manager's <code>checkMemberAccess</code> method with <code>this</code>
  215.      * and <code>Member.PUBLIC</code> as its arguments. If the class is in a
  216.      * package, then this method also calls the security manager's
  217.      * <code>checkPackageAccess</code> method with the package name as its
  218.      * argument. Either of these calls could result in a SecurityException.
  219.      *
  220.      * @return     a newly allocated instance of the class represented by this
  221.      *             object.
  222.      * @exception  IllegalAccessException  if the class or initializer is
  223.      *               not accessible.
  224.      * @exception  InstantiationException 
  225.      *               if this <code>Class</code> represents an abstract class,
  226.      *               an interface, an array class,
  227.      *               a primitive type, or void;
  228.      *               or if the instantiation fails for some other reason.
  229.      * @exception  ExceptionInInitializerError if the initialization
  230.      *               provoked by this method fails.
  231.      * @exception  SecurityException if there is no permission to create a new
  232.      *               instance.
  233.      *
  234.      */
  235.     public Object newInstance() 
  236.         throws InstantiationException, IllegalAccessException
  237.     {
  238.         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
  239.     return newInstance0();
  240.     }
  241.  
  242.     private native Object newInstance0()
  243.         throws InstantiationException, IllegalAccessException;
  244.  
  245.  
  246.     /**
  247.      * Determines if the specified <code>Object</code> is assignment-compatible
  248.      * with the object represented by this <code>Class</code>.  This method is
  249.      * the dynamic equivalent of the Java language <code>instanceof</code>
  250.      * operator. The method returns <code>true</code> if the specified
  251.      * <code>Object</code> argument is non-null and can be cast to the
  252.      * reference type represented by this <code>Class</code> object without
  253.      * raising a <code>ClassCastException.</code> It returns <code>false</code>
  254.      * otherwise.
  255.      *
  256.      * <p> Specifically, if this <code>Class</code> object represents a
  257.      * declared class, this method returns <code>true</code> if the specified
  258.      * <code>Object</code> argument is an instance of the represented class (or
  259.      * of any of its subclasses); it returns <code>false</code> otherwise. If
  260.      * this <code>Class</code> object represents an array class, this method
  261.      * returns <code>true</code> if the specified <code>Object</code> argument
  262.      * can be converted to an object of the array class by an identity
  263.      * conversion or by a widening reference conversion; it returns
  264.      * <code>false</code> otherwise. If this <code>Class</code> object
  265.      * represents an interface, this method returns <code>true</code> if the
  266.      * class or any superclass of the specified <code>Object</code> argument
  267.      * implements this interface; it returns <code>false</code> otherwise. If
  268.      * this <code>Class</code> object represents a primitive type, this method
  269.      * returns <code>false</code>.
  270.      *
  271.      * @param   obj the object to check
  272.      *
  273.      * @since JDK1.1
  274.      */
  275.     public native boolean isInstance(Object obj);
  276.  
  277.  
  278.     /**
  279.      * Determines if the class or interface represented by this
  280.      * <code>Class</code> object is either the same as, or is a superclass or
  281.      * superinterface of, the class or interface represented by the specified
  282.      * <code>Class</code> parameter. It returns <code>true</code> if so;
  283.      * otherwise it returns <code>false</code>. If this <code>Class</code>
  284.      * object represents a primitive type, this method returns
  285.      * <code>true</code> if the specified <code>Class</code> parameter is
  286.      * exactly this <code>Class</code> object; otherwise it returns
  287.      * <code>false</code>.
  288.      *
  289.      * <p> Specifically, this method tests whether the type represented by the
  290.      * specified <code>Class</code> parameter can be converted to the type
  291.      * represented by this <code>Class</code> object via an identity conversion
  292.      * or via a widening reference conversion. See <em>The Java Language
  293.      * Specification</em>, sections 5.1.1 and 5.1.4 , for details.
  294.      *
  295.      * @exception NullPointerException if the specified Class parameter is
  296.      *            null.
  297.      * @since JDK1.1
  298.      */
  299.     public native boolean isAssignableFrom(Class cls);
  300.  
  301.  
  302.     /**
  303.      * Determines if the specified <code>Class</code> object represents an
  304.      * interface type.
  305.      *
  306.      * @return  <code>true</code> if this object represents an interface;
  307.      *          <code>false</code> otherwise.
  308.      */
  309.     public native boolean isInterface();
  310.  
  311.  
  312.     /**
  313.      * Determines if this <code>Class</code> object represents an array class.
  314.      *
  315.      * @return  <code>true</code> if this object represents an array class;
  316.      *          <code>false</code> otherwise.
  317.      * @since   JDK1.1
  318.      */
  319.     public native boolean isArray();
  320.  
  321.  
  322.     /**
  323.      * Determines if the specified <code>Class</code> object represents a
  324.      * primitive type.
  325.      *
  326.      * <p> There are nine predefined <code>Class</code> objects to represent
  327.      * the eight primitive types and void.  These are created by the Java
  328.      * Virtual Machine, and have the same names as the primitive types that
  329.      * they represent, namely <code>boolean</code>, <code>byte</code>,
  330.      * <code>char</code>, <code>short</code>, <code>int</code>,
  331.      * <code>long</code>, <code>float</code>, and <code>double</code>.
  332.      *
  333.      * <p> These objects may only be accessed via the following public static
  334.      * final variables, and are the only <code>Class</code> objects for which
  335.      * this method returns <code>true</code>.
  336.      *
  337.      * @see     java.lang.Boolean#TYPE
  338.      * @see     java.lang.Character#TYPE
  339.      * @see     java.lang.Byte#TYPE
  340.      * @see     java.lang.Short#TYPE
  341.      * @see     java.lang.Integer#TYPE
  342.      * @see     java.lang.Long#TYPE
  343.      * @see     java.lang.Float#TYPE
  344.      * @see     java.lang.Double#TYPE
  345.      * @see     java.lang.Void#TYPE
  346.      * @since JDK1.1
  347.      */
  348.     public native boolean isPrimitive();
  349.  
  350.  
  351.     /**
  352.      * Returns the fully-qualified name of the entity (class, interface, array
  353.      * class, primitive type, or void) represented by this <code>Class</code>
  354.      * object, as a <code>String</code>.
  355.      *
  356.      * <p> If this <code>Class</code> object represents a class of arrays, then
  357.      * the internal form of the name consists of the name of the element type
  358.      * in Java signature format, preceded by one or more "<tt>[</tt>"
  359.      * characters representing the depth of array nesting. Thus:
  360.      *
  361.      * <blockquote><pre>
  362.      * (new Object[3]).getClass().getName()
  363.      * </pre></blockquote>
  364.      *
  365.      * returns "<code>[Ljava.lang.Object;</code>" and:
  366.      *
  367.      * <blockquote><pre>
  368.      * (new int[3][4][5][6][7][8][9]).getClass().getName()
  369.      * </pre></blockquote>
  370.      *
  371.      * returns "<code>[[[[[[[I</code>". The encoding of element type names 
  372.      * is as follows:
  373.      *
  374.      * <blockquote><pre>
  375.      * B            byte
  376.      * C            char
  377.      * D            double
  378.      * F            float
  379.      * I            int
  380.      * J            long
  381.      * L<i>classname;</i>  class or interface
  382.      * S            short
  383.      * Z            boolean
  384.      * </pre></blockquote>
  385.      *
  386.      * The class or interface name <tt><i>classname</i></tt> is given in fully
  387.      * qualified form as shown in the example above.
  388.      *
  389.      * @return  the fully qualified name of the class or interface
  390.      *          represented by this object.
  391.      */
  392.     public native String getName();
  393.  
  394.  
  395.     /**
  396.      * Returns the class loader for the class.  Some implementations may use
  397.      * null to represent the bootstrap class loader. This method will return
  398.      * null in such implementations if this class was loaded by the bootstrap
  399.      * class loader.
  400.      *
  401.      * <p> If a security manager is present, and the caller's class loader is
  402.      * not null and the caller's class loader is not the same as or an ancestor of
  403.      * the class loader for the class whose class loader is requested, then
  404.      * this method calls the security manager's <code>checkPermission</code> 
  405.      * method with a <code>RuntimePermission("getClassLoader")</code> 
  406.      * permission to ensure it's ok to access the class loader for the class.
  407.      * 
  408.      * <p>If this object
  409.      * represents a primitive type or void, null is returned.
  410.      *
  411.      * @return  the class loader that loaded the class or interface
  412.      *          represented by this object.
  413.      * @throws SecurityException
  414.      *    if a security manager exists and its 
  415.      *    <code>checkPermission</code> method denies
  416.      *    access to the class loader for the class.
  417.      * @see java.lang.ClassLoader
  418.      * @see SecurityManager#checkPermission
  419.      * @see java.lang.RuntimePermission
  420.      */
  421.     public ClassLoader getClassLoader() {
  422.         ClassLoader cl = getClassLoader0();
  423.         if (cl == null)
  424.             return null;
  425.         SecurityManager sm = System.getSecurityManager();
  426.         if (sm != null) {
  427.             ClassLoader ccl = ClassLoader.getCallerClassLoader();
  428.             if (ccl != null && ccl != cl && !cl.isAncestor(ccl)) {
  429.                 sm.checkPermission(ClassLoader.getGetClassLoaderPerm());
  430.             }
  431.         }
  432.         return cl;
  433.     }
  434.  
  435.  
  436.     private native ClassLoader getClassLoader0();
  437.  
  438.  
  439.     /**
  440.      * Returns the <code>Class</code> representing the superclass of the entity
  441.      * (class, interface, primitive type or void) represented by this
  442.      * <code>Class</code>.  If this <code>Class</code> represents either the
  443.      * <code>Object</code> class, an interface, a primitive type, or void, then
  444.      * null is returned.  If this object represents an array class then the
  445.      * <code>Class</code> object representing the <code>Object</code> class is
  446.      * returned.
  447.      *
  448.      * @return the superclass of the class represented by this object.
  449.      */
  450.     public native Class getSuperclass();
  451.  
  452.  
  453.     /**
  454.      * Gets the package for this class.  The class loader of this class is used
  455.      * to find the package.  If the class was loaded by the bootstrap class
  456.      * loader the set of packages loaded from CLASSPATH is searched to find the
  457.      * package of the class. Null is returned if no package object was created
  458.      * by the class loader of this class.
  459.      *
  460.      * <p> Packages have attributes for versions and specifications only if the
  461.      * information was defined in the manifests that accompany the classes, and
  462.      * if the class loader created the package instance with the attributes
  463.      * from the manifest.
  464.      *
  465.      * @return the package of the class, or null if no package
  466.      *         information is available from the archive or codebase.
  467.      */
  468.     public Package getPackage() {
  469.         return Package.getPackage(this);
  470.     }
  471.  
  472.  
  473.     /**
  474.      * Determines the interfaces implemented by the class or interface
  475.      * represented by this object.
  476.      *
  477.      * <p> If this object represents a class, the return value is an array
  478.      * containing objects representing all interfaces implemented by the
  479.      * class. The order of the interface objects in the array corresponds to
  480.      * the order of the interface names in the <code>implements</code> clause
  481.      * of the declaration of the class represented by this object. For 
  482.      * example, given the declaration:
  483.      * <blockquote><pre>
  484.      * class Shimmer implements FloorWax, DessertTopping { ... }
  485.      * </pre></blockquote>
  486.      * suppose the value of <code>s</code> is an instance of 
  487.      * <code>Shimmer</code>; the value of the expression:
  488.      * <blockquote><pre>
  489.      * s.getClass().getInterfaces()[0]
  490.      * </pre></blockquote>
  491.      * is the <code>Class</code> object that represents interface 
  492.      * <code>FloorWax</code>; and the value of:
  493.      * <blockquote><pre>
  494.      * s.getClass().getInterfaces()[1]
  495.      * </pre></blockquote>
  496.      * is the <code>Class</code> object that represents interface 
  497.      * <code>DessertTopping</code>.
  498.      *
  499.      * <p> If this object represents an interface, the array contains objects
  500.      * representing all interfaces extended by the interface. The order of the
  501.      * interface objects in the array corresponds to the order of the interface
  502.      * names in the <code>extends</code> clause of the declaration of the
  503.      * interface represented by this object.
  504.      *
  505.      * <p> If this object represents a class or interface that implements no
  506.      * interfaces, the method returns an array of length 0.
  507.      *
  508.      * <p> If this object represents a primitive type or void, the method
  509.      * returns an array of length 0.
  510.      *
  511.      * @return an array of interfaces implemented by this class.
  512.      */
  513.     public native Class[] getInterfaces();
  514.  
  515.  
  516.     /**
  517.      * Returns the <code>Class</code> representing the component type of an
  518.      * array.  If this class does not represent an array class this method
  519.      * returns null.
  520.      *
  521.      * @see     java.lang.reflect.Array
  522.      * @since JDK1.1
  523.      */
  524.     public native Class getComponentType();
  525.  
  526.  
  527.     /**
  528.      * Returns the Java language modifiers for this class or interface, encoded
  529.      * in an integer. The modifiers consist of the Java Virtual Machine's
  530.      * constants for <code>public</code>, <code>protected</code>,
  531.      * <code>private</code>, <code>final</code>, <code>static</code>,
  532.      * <code>abstract</code> and <code>interface</code>; they should be decoded
  533.      * using the methods of class <code>Modifier</code>.
  534.      *
  535.      * <p> If the underlying class is an array class, then its
  536.      * <code>public</code>, <code>private</code> and <code>protected</code>
  537.      * modifiers are the same as those of its component type.  If this
  538.      * <code>Class</code> represents a primitive type or void, its
  539.      * <code>public</code> modifier is always <code>true</code>, and its
  540.      * <code>protected</code> and <code>private</code> modifers are always
  541.      * <code>false</code>. If this object represents an array class, a
  542.      * primitive type or void, then its <code>final</code> modifier is always
  543.      * <code>true</code> and its interface modifer is always
  544.      * <code>false</code>. The values of its other modifiers are not determined
  545.      * by this specification.
  546.      *
  547.      * <p> The modifier encodings are defined in <em>The Java Virtual Machine
  548.      * Specification</em>, table 4.1.
  549.      *
  550.      * @see     java.lang.reflect.Modifier
  551.      * @since JDK1.1
  552.      */
  553.     public native int getModifiers();
  554.  
  555.  
  556.     /**
  557.      * Gets the signers of this class.
  558.      *
  559.      * @return  the signers of this class, or null if there are no signers.  In
  560.      *         particular, this method returns null if this object represents
  561.      *         a primitive type or void.
  562.      * @since     JDK1.1
  563.      */
  564.     public native Object[] getSigners();
  565.         
  566.  
  567.     /**
  568.      * Set the signers of this class.
  569.      */
  570.     native void setSigners(Object[] signers);
  571.  
  572.  
  573.     /**
  574.      * If the class or interface represented by this <code>Class</code> object
  575.      * is a member of another class, returns the <code>Class</code> object
  576.      * representing the class in which it was declared.  This method returns
  577.      * null if this class or interface is not a member of any other class.  If
  578.      * this <code>Class</code> object represents an array class, a primitive
  579.      * type, or void,then this method returns null.
  580.      *
  581.      * @since JDK1.1
  582.      */
  583.     public native Class getDeclaringClass();
  584.  
  585.  
  586.     /**
  587.      * Returns an array containing <code>Class</code> objects representing all
  588.      * the public classes and interfaces that are members of the class
  589.      * represented by this <code>Class</code> object.  This includes public
  590.      * class and interface members inherited from superclasses and public class
  591.      * and interface members declared by the class.  This method returns an
  592.      * array of length 0 if this <code>Class</code> object has no public member
  593.      * classes or interfaces.  This method also returns an array of length 0 if
  594.      * this <code>Class</code> object represents a primitive type, an array
  595.      * class, or void.
  596.      * 
  597.      * <p>For this class and each of its superclasses, the following
  598.      * security checks are performed:
  599.      * If there is a security manager, the security manager's 
  600.      * <code>checkMemberAccess</code> method is called
  601.      * with <code>this</code> and <code>Member.DECLARED</code> 
  602.      * as its arguments, where <code>this</code> is this class or the superclass
  603.      * whose members are being determined. If the class is in a package, then 
  604.      * the security manager's <code>checkPackageAccess</code> 
  605.      * method is also called with the package name 
  606.      * as its argument. Either of these calls could result in a SecurityException.
  607.      *
  608.      * @exception SecurityException    if access to the information is denied.
  609.      * @see       SecurityManager#checkMemberAccess(Class, int)
  610.      * @see       SecurityManager#checkPackageAccess(String)
  611.      *
  612.      * @since JDK1.1
  613.      */
  614.     public Class[] getClasses() {
  615.         /* simplest implementation. */
  616.         java.util.Vector v = new java.util.Vector();
  617.         Class currentClass = this;
  618.         while (currentClass != null) {
  619.             Class[] members = currentClass.getDeclaredClasses();
  620.             for (int i = 0; i < members.length; i++) {
  621.                 if (Modifier.isPublic(members[i].getModifiers())) {
  622.                     v.add(members[i]);
  623.                 }
  624.             }
  625.             currentClass = currentClass.getSuperclass();
  626.         }
  627.         Class[] ret = new Class[v.size()];
  628.         int i = 0;
  629.         for (java.util.Enumeration e = v.elements(); e.hasMoreElements();) {
  630.             ret[i++] = (Class)e.nextElement();
  631.         }
  632.         return ret;
  633.     }
  634.  
  635.  
  636.     /**
  637.      * Returns an array containing <code>Field</code> objects reflecting all
  638.      * the accessible public fields of the class or interface represented by
  639.      * this <code>Class</code> object.  The elements in the array returned are
  640.      * not sorted and are not in any particular order.  This method returns an
  641.      * array of length 0 if the class or interface has no accessible public
  642.      * fields, or if it represents an array class, a primitive type, or void.
  643.      *
  644.      * <p> Specifically, if this <code>Class</code> object represents a class,
  645.      * this method returns the public fields of this class and of all its
  646.      * superclasses.  If this <code>Class</code> object represents an
  647.      * interface, this method returns the fields of this interface and of all
  648.      * its superinterfaces.
  649.      *
  650.      * <p>If there is a security manager, this method first
  651.      * calls the security manager's <code>checkMemberAccess</code> method
  652.      * with <code>this</code> and <code>Member.PUBLIC</code> 
  653.      * as its arguments. If the class is in a package, then this method
  654.      * also calls the security manager's <code>checkPackageAccess</code> 
  655.      * method with the package name 
  656.      * as its argument. Either of these calls could result in a SecurityException.
  657.      * 
  658.      * <p> The implicit length field for array classs is not reflected by this
  659.      * method. User code should use the methods of class <code>Array</code> to
  660.      * manipulate arrays.
  661.      *
  662.      * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
  663.      *
  664.      * @exception SecurityException    if access to the information is denied.
  665.      * @see       java.lang.reflect.Field
  666.      * @see       SecurityManager#checkMemberAccess(Class, int)
  667.      * @see       SecurityManager#checkPackageAccess(String)
  668.      * @since JDK1.1
  669.      */
  670.     public Field[] getFields() throws SecurityException {
  671.     // be very careful not to change the stack depth of this
  672.     // checkMemberAccess call for security reasons 
  673.     // see java.lang.SecurityManager.checkMemberAccess
  674.         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
  675.         return getFields0(Member.PUBLIC);
  676.     }
  677.  
  678.  
  679.     /**
  680.      * Returns an array containing <code>Method</code> objects reflecting all
  681.      * the public <em>member</em> methods of the class or interface represented
  682.      * by this <code>Class</code> object, including those declared by the class
  683.      * or interface and and those inherited from superclasses and
  684.      * superinterfaces.  The elements in the array returned are not sorted and
  685.      * are not in any particular order.  This method returns an array of length
  686.      * 0 if this <code>Class</code> object represents a class or interface that
  687.      * has no public member methods, or if this <code>Class</code> object
  688.      * represents an array class, primitive type, or void.
  689.      *
  690.      * <p>If there is a security manager, this method first
  691.      * calls the security manager's <code>checkMemberAccess</code> method
  692.      * with <code>this</code> and <code>Member.PUBLIC</code> 
  693.      * as its arguments. If the class is in a package, then this method
  694.      * also calls the security manager's <code>checkPackageAccess</code> 
  695.      * method with the package name 
  696.      * as its argument. Either of these calls could result in a SecurityException.
  697.      * 
  698.      * <p> The class initialization method <code><clinit></code> is not
  699.      * included in the returned array. If the class declares multiple public
  700.      * member methods with the same parameter types, they are all included in
  701.      * the returned array.
  702.      *
  703.      * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
  704.      *
  705.      * @exception SecurityException    if access to the information is denied.
  706.      * @see       java.lang.reflect.Method
  707.      * @see       SecurityManager#checkMemberAccess(Class, int)
  708.      * @see       SecurityManager#checkPackageAccess(String)
  709.      * @since JDK1.1
  710.      */
  711.     public Method[] getMethods() throws SecurityException {
  712.     // be very careful not to change the stack depth of this
  713.     // checkMemberAccess call for security reasons 
  714.     // see java.lang.SecurityManager.checkMemberAccess
  715.         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
  716.         return getMethods0(Member.PUBLIC);
  717.     }
  718.  
  719.  
  720.     /**
  721.      * Returns an array containing <code>Constructor</code> objects reflecting
  722.      * all the public constructors of the class represented by this
  723.      * <code>Class</code> object.  An array of length 0 is returned if the
  724.      * class has no public constructors, or if the class is an array class, or
  725.      * if the class reflects a primitive type or void.
  726.      *
  727.      * <p>If there is a security manager, this method first
  728.      * calls the security manager's <code>checkMemberAccess</code> method
  729.      * with <code>this</code> and <code>Member.PUBLIC</code> 
  730.      * as its arguments. If the class is in a package, then this method
  731.      * also calls the security manager's <code>checkPackageAccess</code> 
  732.      * method with the package name 
  733.      * as its argument. Either of these calls could result in a SecurityException.
  734.      * 
  735.      * @exception SecurityException    if access to the information is denied.
  736.      * @see       java.lang.reflect.Constructor
  737.      * @see       SecurityManager#checkMemberAccess(Class, int)
  738.      * @see       SecurityManager#checkPackageAccess(String)
  739.      * @since JDK1.1
  740.      */
  741.     public Constructor[] getConstructors() throws SecurityException {
  742.     // be very careful not to change the stack depth of this
  743.     // checkMemberAccess call for security reasons 
  744.     // see java.lang.SecurityManager.checkMemberAccess
  745.         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
  746.         return getConstructors0(Member.PUBLIC);
  747.     }
  748.  
  749.  
  750.     /**
  751.      * Returns a <code>Field</code> object that reflects the specified public
  752.      * member field of the class or interface represented by this
  753.      * <code>Class</code> object. The <code>name</code> parameter is a
  754.      * <code>String</code> specifying the simple name of the desired field.
  755.      *
  756.      * <p>If there is a security manager, this method first
  757.      * calls the security manager's <code>checkMemberAccess</code> method
  758.      * with <code>this</code> and <code>Member.PUBLIC</code> 
  759.      * as its arguments. If the class is in a package, then this method
  760.      * also calls the security manager's <code>checkPackageAccess</code> 
  761.      * method with the package name 
  762.      * as its argument. Either of these calls could result in a SecurityException.
  763.      *
  764.      * <p> The field to be reflected is determined by the algorithm that
  765.      * follows.  Let C be the class represented by this object:
  766.      * <OL>
  767.      * <LI> If C declares a public field with the name specified, that is the
  768.      *      field to be reflected.</LI>
  769.      * <LI> If no field was found in step 1 above, this algorithm is applied
  770.      *         recursively to each direct superinterface of C. The direct
  771.      *         superinterfaces are searched in the order they were declared.</LI>
  772.      * <LI> If no field was found in steps 1 and 2 above, and C has a
  773.      *      superclass S, then this algorithm is invoked recursively upon S.
  774.      *      If C has no superclass, then a <code>NoSuchFieldException</code>
  775.      *      is thrown.</LI>
  776.      * </OL>
  777.      *
  778.      * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
  779.      * 
  780.      * @exception NoSuchFieldException if a field with the specified name is
  781.      *              not found.
  782.      * @exception SecurityException    if access to the information is denied.
  783.      * @see       java.lang.reflect.Field
  784.      * @see       SecurityManager#checkMemberAccess(Class, int)
  785.      * @see       SecurityManager#checkPackageAccess(String)
  786.      * @since JDK1.1
  787.      */
  788.     public Field getField(String name)
  789.         throws NoSuchFieldException, SecurityException {
  790.     // be very careful not to change the stack depth of this
  791.     // checkMemberAccess call for security reasons 
  792.     // see java.lang.SecurityManager.checkMemberAccess
  793.         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
  794.         return getField0(name, Member.PUBLIC);
  795.     }
  796.  
  797.  
  798.     /**
  799.      * Returns a <code>Method</code> object that reflects the specified public
  800.      * member method of the class or interface represented by this
  801.      * <code>Class</code> object. The <code>name</code> parameter is a
  802.      * <code>String</code> specifying the simple name the desired method. The
  803.      * <code>parameterTypes</code> parameter is an array of <code>Class</code>
  804.      * objects that identify the method's formal parameter types, in declared
  805.      * order. If <code>parameterTypes</code> is <code>null</code>, it is 
  806.      * treated as if it were an empty array.
  807.      *
  808.      * <p>If there is a security manager, this method first
  809.      * calls the security manager's <code>checkMemberAccess</code> method
  810.      * with <code>this</code> and <code>Member.PUBLIC</code> 
  811.      * as its arguments. If the class is in a package, then this method
  812.      * also calls the security manager's <code>checkPackageAccess</code> 
  813.      * method with the package name 
  814.      * as its argument. Either of these calls could result in a SecurityException.
  815.      *
  816.      * <p> If the <code>name</code> is "<init>"or "<clinit>" a
  817.      * <code>NoSuchMethodException</code> is raised. Otherwise, the method to
  818.      * be reflected is determined by the algorithm that follows.  Let C be the
  819.      * class represented by this object:
  820.      * <OL>
  821.      * <LI> C is searched for any <I>matching methods</I>. If no matching
  822.      *         method is found, the algorithm of step 1 is invoked recursively on
  823.      *         the superclass of C.</LI>
  824.      * <LI> If no method was found in step 1 above, the superinterfaces of C
  825.      *      are searched for a matching method. If any such method is found, it
  826.      *      is reflected.</LI>
  827.      * </OL>
  828.      *
  829.      * To find a matching method in a class C:  If C declares exactly one
  830.      * public method with the specified name and exactly the same formal
  831.      * parameter types, that is the method reflected. If more than one such
  832.      * method is found in C, and one of these methods has a return type that is
  833.      * more specific than any of the others, that method is reflected;
  834.      * otherwise one of the methods is chosen arbitrarily.
  835.      *
  836.      * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
  837.      *
  838.      * @exception NoSuchMethodException if a matching method is not found
  839.      *            or if then name is "<init>"or "<clinit>".
  840.      * @exception SecurityException    if access to the information is denied.
  841.      * @see       java.lang.reflect.Method
  842.      * @see       SecurityManager#checkMemberAccess(Class, int)
  843.      * @see       SecurityManager#checkPackageAccess(String)
  844.      * @since JDK1.1
  845.      */
  846.     public Method getMethod(String name, Class[] parameterTypes)
  847.         throws NoSuchMethodException, SecurityException {
  848.     // be very careful not to change the stack depth of this
  849.     // checkMemberAccess call for security reasons 
  850.     // see java.lang.SecurityManager.checkMemberAccess
  851.         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
  852.         return getMethod0(name, parameterTypes, Member.PUBLIC);
  853.     }
  854.  
  855.  
  856.     /**
  857.      * Returns a <code>Constructor</code> object that reflects the specified
  858.      * public constructor of the class represented by this <code>Class</code>
  859.      * object. The <code>parameterTypes</code> parameter is an array of
  860.      * <code>Class</code> objects that identify the constructor's formal
  861.      * parameter types, in declared order.
  862.      *
  863.      * <p> The constructor to reflect is the public constructor of the class
  864.      * represented by this <code>Class</code> object whose formal parameter
  865.      * types match those specified by <code>parameterTypes</code>.
  866.      *
  867.      * <p>If there is a security manager, this method first
  868.      * calls the security manager's <code>checkMemberAccess</code> method
  869.      * with <code>this</code> and <code>Member.PUBLIC</code> 
  870.      * as its arguments. If the class is in a package, then this method
  871.      * also calls the security manager's <code>checkPackageAccess</code> 
  872.      * method with the package name 
  873.      * as its argument. Either of these calls could result in a SecurityException.
  874.      *
  875.      * @exception NoSuchMethodException if a matching method is not found.
  876.      * @exception SecurityException     if access to the information is denied.
  877.      * @see       java.lang.reflect.Constructor
  878.      * @see       SecurityManager#checkMemberAccess(Class, int)
  879.      * @see       SecurityManager#checkPackageAccess(String)
  880.      * @since JDK1.1
  881.      */
  882.     public Constructor getConstructor(Class[] parameterTypes)
  883.         throws NoSuchMethodException, SecurityException {
  884.     // be very careful not to change the stack depth of this
  885.     // checkMemberAccess call for security reasons 
  886.     // see java.lang.SecurityManager.checkMemberAccess
  887.         checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
  888.         return getConstructor0(parameterTypes, Member.PUBLIC);
  889.     }
  890.  
  891.  
  892.     /**
  893.      * Returns an array of <code>Class</code> objects reflecting all the
  894.      * classes and interfaces declared as members of the class represented by
  895.      * this <code>Class</code> object. This includes public, protected, default
  896.      * (package) access, and private classes and interfaces declared by the
  897.      * class, but excludes inherited classes and interfaces.  This method
  898.      * returns an array of length 0 if the class declares no classes or
  899.      * interfaces as members, or if this <code>Class</code> object represents a
  900.      * primitive type, an array class, or void.
  901.      *
  902.      * <p>If there is a security manager, this method first
  903.      * calls the security manager's <code>checkMemberAccess</code> method
  904.      * with <code>this</code> and <code>Member.DECLARED</code> 
  905.      * as its arguments. If the class is in a package, then this method
  906.      * also calls the security manager's <code>checkPackageAccess</code> 
  907.      * method with the package name 
  908.      * as its argument. Either of these calls could result in a SecurityException.
  909.      *
  910.      * @exception SecurityException    if access to the information is denied.
  911.      * @see       SecurityManager#checkMemberAccess(Class, int)
  912.      * @see       SecurityManager#checkPackageAccess(String)
  913.      * @since JDK1.1
  914.      */
  915.     public Class[] getDeclaredClasses() throws SecurityException {
  916.     // be very careful not to change the stack depth of this
  917.     // checkMemberAccess call for security reasons 
  918.     // see java.lang.SecurityManager.checkMemberAccess
  919.         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
  920.         return getDeclaredClasses0();
  921.     }
  922.  
  923.  
  924.     /**
  925.      * Returns an array of <code>Field</code> objects reflecting all the fields
  926.      * declared by the class or interface represented by this
  927.      * <code>Class</code> object. This includes public, protected, default
  928.      * (package) access, and private fields, but excludes inherited fields.
  929.      * The elements in the array returned are not sorted and are not in any
  930.      * particular order.  This method returns an array of length 0 if the class
  931.      * or interface declares no fields, or if this <code>Class</code> object
  932.      * represents a primitive type, an array class, or void.
  933.      *
  934.      * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
  935.      *
  936.      * <p>If there is a security manager, this method first
  937.      * calls the security manager's <code>checkMemberAccess</code> method
  938.      * with <code>this</code> and <code>Member.DECLARED</code> 
  939.      * as its arguments. If the class is in a package, then this method
  940.      * also calls the security manager's <code>checkPackageAccess</code> 
  941.      * method with the package name 
  942.      * as its argument. Either of these calls could result in a SecurityException.
  943.      *
  944.      * @exception SecurityException    if access to the information is denied.
  945.      * @see       java.lang.reflect.Field
  946.      * @see       SecurityManager#checkMemberAccess(Class, int)
  947.      * @see       SecurityManager#checkPackageAccess(String)
  948.      * @since JDK1.1
  949.      */
  950.     public Field[] getDeclaredFields() throws SecurityException {
  951.     // be very careful not to change the stack depth of this
  952.     // checkMemberAccess call for security reasons 
  953.     // see java.lang.SecurityManager.checkMemberAccess
  954.         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
  955.         return getFields0(Member.DECLARED);
  956.     }
  957.  
  958.  
  959.     /**
  960.      * Returns an array of <code>Method</code> objects reflecting all the
  961.      * methods declared by the class or interface represented by this
  962.      * <code>Class</code> object. This includes public, protected, default
  963.      * (package) access, and private methods, but excludes inherited methods.
  964.      * The elements in the array returned are not sorted and are not in any
  965.      * particular order.  This method returns an array of length 0 if the class
  966.      * or interface declares no methods, or if this <code>Class</code> object
  967.      * represents a primitive type, an array class, or void.  The class
  968.      * initialization method <code><clinit></code> is not included in the
  969.      * returned array. If the class declares multiple public member methods
  970.      * with the same parameter types, they are all included in the returned
  971.      * array.
  972.      *
  973.      * <p> See <em>The Java Language Specification</em>, section 8.2.
  974.      *
  975.      * <p>If there is a security manager, this method first
  976.      * calls the security manager's <code>checkMemberAccess</code> method
  977.      * with <code>this</code> and <code>Member.DECLARED</code> 
  978.      * as its arguments. If the class is in a package, then this method
  979.      * also calls the security manager's <code>checkPackageAccess</code> 
  980.      * method with the package name 
  981.      * as its argument. Either of these calls could result in a SecurityException.
  982.      *
  983.      * @exception SecurityException    if access to the information is denied.
  984.      * @see       java.lang.reflect.Method
  985.      * @see       SecurityManager#checkMemberAccess(Class, int)
  986.      * @see       SecurityManager#checkPackageAccess(String)
  987.      * @since JDK1.1
  988.      */
  989.     public Method[] getDeclaredMethods() throws SecurityException {
  990.     // be very careful not to change the stack depth of this
  991.     // checkMemberAccess call for security reasons 
  992.     // see java.lang.SecurityManager.checkMemberAccess
  993.         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
  994.         return getMethods0(Member.DECLARED);
  995.     }
  996.  
  997.  
  998.     /**
  999.      * Returns an array of <code>Constructor</code> objects reflecting all the
  1000.      * constructors declared by the class represented by this
  1001.      * <code>Class</code> object. These are public, protected, default
  1002.      * (package) access, and private constructors.  The elements in the array
  1003.      * returned are not sorted and are not in any particular order.  If the
  1004.      * class has a default constructor, it is included in the returned array.
  1005.      * This method returns an array of length 0 if this <code>Class</code>
  1006.      * object represents an interface, a primitive type, an array class, or
  1007.      * void.
  1008.      *
  1009.      * <p> See <em>The Java Language Specification</em>, section 8.2.
  1010.      *
  1011.      * <p>If there is a security manager, this method first
  1012.      * calls the security manager's <code>checkMemberAccess</code> method
  1013.      * with <code>this</code> and <code>Member.DECLARED</code> 
  1014.      * as its arguments. If the class is in a package, then this method
  1015.      * also calls the security manager's <code>checkPackageAccess</code> 
  1016.      * method with the package name 
  1017.      * as its argument. Either of these calls could result in a SecurityException.
  1018.      *
  1019.      * @exception SecurityException    if access to the information is denied.
  1020.      * @see       java.lang.reflect.Constructor
  1021.      * @see       SecurityManager#checkMemberAccess(Class, int)
  1022.      * @see       SecurityManager#checkPackageAccess(String)
  1023.      * @since JDK1.1
  1024.      */
  1025.     public Constructor[] getDeclaredConstructors() throws SecurityException {
  1026.     // be very careful not to change the stack depth of this
  1027.     // checkMemberAccess call for security reasons 
  1028.     // see java.lang.SecurityManager.checkMemberAccess
  1029.         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
  1030.         return getConstructors0(Member.DECLARED);
  1031.     }
  1032.  
  1033.  
  1034.     /**
  1035.      * Returns a <code>Field</code> object that reflects the specified declared
  1036.      * field of the class or interface represented by this <code>Class</code>
  1037.      * object. The <code>name</code> parameter is a <code>String</code> that
  1038.      * specifies the simple name of the desired field.  Note that this method
  1039.      * will not reflect the <code>length</code> field of an array class.
  1040.      *
  1041.      * <p>If there is a security manager, this method first
  1042.      * calls the security manager's <code>checkMemberAccess</code> method
  1043.      * with <code>this</code> and <code>Member.DECLARED</code> 
  1044.      * as its arguments. If the class is in a package, then this method
  1045.      * also calls the security manager's <code>checkPackageAccess</code> 
  1046.      * method with the package name 
  1047.      * as its argument. Either of these calls could result in a SecurityException.
  1048.      *
  1049.      * @exception NoSuchFieldException if a field with the specified name is
  1050.      *              not found.
  1051.      * @exception SecurityException    if access to the information is denied.
  1052.      * @see       java.lang.reflect.Field
  1053.      * @see       SecurityManager#checkMemberAccess(Class, int)
  1054.      * @see       SecurityManager#checkPackageAccess(String)
  1055.      * @since JDK1.1
  1056.      */
  1057.     public Field getDeclaredField(String name)
  1058.         throws NoSuchFieldException, SecurityException {
  1059.     // be very careful not to change the stack depth of this
  1060.     // checkMemberAccess call for security reasons 
  1061.     // see java.lang.SecurityManager.checkMemberAccess
  1062.         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
  1063.         return getField0(name, Member.DECLARED);
  1064.     }
  1065.  
  1066.  
  1067.     /**
  1068.      * Returns a <code>Method</code> object that reflects the specified
  1069.      * declared method of the class or interface represented by this
  1070.      * <code>Class</code> object. The <code>name</code> parameter is a
  1071.      * <code>String</code> that specifies the simple name of the desired
  1072.      * method, and the <code>parameterTypes</code> parameter is an array of
  1073.      * <code>Class</code> objects that identify the method's formal parameter
  1074.      * types, in declared order.  If more than one method with the same
  1075.      * parameter types is declared in a class, and one of these methods has a
  1076.      * return type that is more specific than any of the others, that method is
  1077.      * returned; otherwise one of the methods is chosen arbitrarily.  If the
  1078.      * name is "<init>"or "<clinit>" a <code>NoSuchMethodException</code>
  1079.      * is raised.
  1080.      *
  1081.      * <p>If there is a security manager, this method first
  1082.      * calls the security manager's <code>checkMemberAccess</code> method
  1083.      * with <code>this</code> and <code>Member.DECLARED</code> 
  1084.      * as its arguments. If the class is in a package, then this method
  1085.      * also calls the security manager's <code>checkPackageAccess</code> 
  1086.      * method with the package name 
  1087.      * as its argument. Either of these calls could result in a SecurityException.
  1088.      *
  1089.      * @exception NoSuchMethodException if a matching method is not found.
  1090.      * @exception SecurityException     if access to the information is denied.
  1091.      * @see       java.lang.reflect.Method
  1092.      * @see       SecurityManager#checkMemberAccess(Class, int)
  1093.      * @see       SecurityManager#checkPackageAccess(String)
  1094.      * @since JDK1.1
  1095.      */
  1096.     public Method getDeclaredMethod(String name, Class[] parameterTypes)
  1097.         throws NoSuchMethodException, SecurityException {
  1098.     // be very careful not to change the stack depth of this
  1099.     // checkMemberAccess call for security reasons 
  1100.     // see java.lang.SecurityManager.checkMemberAccess
  1101.         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
  1102.         return getMethod0(name, parameterTypes, Member.DECLARED);
  1103.     }
  1104.  
  1105.  
  1106.     /**
  1107.      * Returns a <code>Constructor</code> object that reflects the specified
  1108.      * constructor of the class or interface represented by this
  1109.      * <code>Class</code> object.  The <code>parameterTypes</code> parameter is
  1110.      * an array of <code>Class</code> objects that identify the constructor's
  1111.      * formal parameter types, in declared order.
  1112.      *
  1113.      * <p>If there is a security manager, this method first
  1114.      * calls the security manager's <code>checkMemberAccess</code> method
  1115.      * with <code>this</code> and <code>Member.DECLARED</code> 
  1116.      * as its arguments. If the class is in a package, then this method
  1117.      * also calls the security manager's <code>checkPackageAccess</code> 
  1118.      * method with the package name 
  1119.      * as its argument. Either of these calls could result in a SecurityException.
  1120.      *
  1121.      * @exception NoSuchMethodException if a matching method is not found.
  1122.      * @exception SecurityException     if access to the information is denied.
  1123.      * @see       java.lang.reflect.Constructor
  1124.      * @see       SecurityManager#checkMemberAccess(Class, int)
  1125.      * @see       SecurityManager#checkPackageAccess(String)
  1126.      * @since JDK1.1
  1127.      */
  1128.     public Constructor getDeclaredConstructor(Class[] parameterTypes)
  1129.         throws NoSuchMethodException, SecurityException {
  1130.     // be very careful not to change the stack depth of this
  1131.     // checkMemberAccess call for security reasons 
  1132.     // see java.lang.SecurityManager.checkMemberAccess
  1133.         checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
  1134.         return getConstructor0(parameterTypes, Member.DECLARED);
  1135.     }
  1136.  
  1137.  
  1138.     /**
  1139.      * Finds a resource with a given name.  This method returns null if no
  1140.      * resource with this name is found.  The rules for searching
  1141.      * resources associated with a given class are implemented by the
  1142.      * defining class loader of the class.
  1143.      *
  1144.      * <p> This method delegates the call to its class loader, after making
  1145.      * these changes to the resource name: if the resource name starts with
  1146.      * "/", it is unchanged; otherwise, the package name is prepended to the
  1147.      * resource name after converting "." to "/".  If this object was loaded by
  1148.      * the bootstrap loader, the call is delegated to
  1149.      * <code>ClassLoader.getSystemResourceAsStream</code>.
  1150.      *
  1151.      * @param name  name of the desired resource
  1152.      * @return      a <code>java.io.InputStream</code> object.
  1153.      * @see         java.lang.ClassLoader
  1154.      * @since JDK1.1
  1155.      */
  1156.     public InputStream getResourceAsStream(String name) {
  1157.         name = resolveName(name);
  1158.         ClassLoader cl = getClassLoader0();
  1159.         if (cl==null) {
  1160.             // A system class.
  1161.             return ClassLoader.getSystemResourceAsStream(name);
  1162.         }
  1163.         return cl.getResourceAsStream(name);
  1164.     }
  1165.  
  1166.  
  1167.     /**
  1168.      * Finds a resource with a given name.  This method returns null if no
  1169.      * resource with this name is found.  The rules for searching resources
  1170.      * associated with a given class are implemented by the * defining class
  1171.      * loader of the class.
  1172.      *
  1173.      * <p> This method delegates the call to its class loader, after making
  1174.      * these changes to the resource name: if the resource name starts with
  1175.      * "/", it is unchanged; otherwise, the package name is prepended to the
  1176.      * resource name after converting "." to "/".  If this object was loaded by
  1177.      * the bootstrap loader, the call is delegated to
  1178.      * <code>ClassLoader.getSystemResource</code>.
  1179.      *
  1180.      * @param name  name of the desired resource
  1181.      * @return      a <code>java.net.URL</code> object.
  1182.      * @see         java.lang.ClassLoader
  1183.      * @since JDK1.1
  1184.      */
  1185.     public java.net.URL getResource(String name) {
  1186.         name = resolveName(name);
  1187.         ClassLoader cl = getClassLoader0();
  1188.         if (cl==null) {
  1189.             // A system class.
  1190.             return ClassLoader.getSystemResource(name);
  1191.         }
  1192.         return cl.getResource(name);
  1193.     }
  1194.  
  1195.  
  1196.     /** permission required to get a protection domain */
  1197.     private static RuntimePermission getPDperm;
  1198.  
  1199.  
  1200.     /** protection domain returned when the internal domain is null */
  1201.     private static java.security.ProtectionDomain allPermDomain;
  1202.  
  1203.  
  1204.     /**
  1205.      * Returns the <code>ProtectionDomain</code> of this class.  If there is a
  1206.      * security manager installed, this method first calls the security
  1207.      * manager's <code>checkPermission</code> method with a
  1208.      * <code>RuntimePermission("getProtectionDomain")</code> permission to
  1209.      * ensure it's ok to get the
  1210.      * <code>ProtectionDomain</code>.
  1211.      *
  1212.      * @return the ProtectionDomain of this class
  1213.      *
  1214.      * @throws SecurityException
  1215.      *        if a security manager exists and its 
  1216.      *        <code>checkPermission</code> method doesn't allow 
  1217.      *        geting the ProtectionDomain.
  1218.      *
  1219.      * @see java.security.ProtectionDomain
  1220.      * @see SecurityManager#checkPermission
  1221.      * @see java.lang.RuntimePermission
  1222.      * @since JDK1.2
  1223.      */
  1224.     public java.security.ProtectionDomain getProtectionDomain() {
  1225.         SecurityManager sm = System.getSecurityManager();
  1226.         if (sm != null) {
  1227.             if (getPDperm == null)
  1228.                 getPDperm = new RuntimePermission("getProtectionDomain");
  1229.             sm.checkPermission(getPDperm);
  1230.         }
  1231.         java.security.ProtectionDomain pd = getProtectionDomain0();
  1232.         if (pd == null) {
  1233.             if (allPermDomain == null) {
  1234.                 java.security.Permissions perms = 
  1235.                     new java.security.Permissions();
  1236.                 perms.add(new java.security.AllPermission());
  1237.                 allPermDomain = 
  1238.                     new java.security.ProtectionDomain(null, perms);
  1239.             }
  1240.             pd = allPermDomain;
  1241.         }
  1242.         return pd;
  1243.     }
  1244.  
  1245.  
  1246.     /**
  1247.      * Returns the ProtectionDomain of this class.
  1248.      */
  1249.     private native java.security.ProtectionDomain getProtectionDomain0();
  1250.  
  1251.  
  1252.     /**
  1253.      * Set the ProtectionDomain for this class. Called by
  1254.      * ClassLoader.defineClass.
  1255.      */
  1256.     native void setProtectionDomain0(java.security.ProtectionDomain pd);
  1257.  
  1258.  
  1259.     /*
  1260.      * Return the Virtual Machine's Class object for the named
  1261.      * primitive type.
  1262.      */
  1263.     static native Class getPrimitiveClass(String name);
  1264.  
  1265.  
  1266.     /*
  1267.      * Check if client is allowed to access members.  If access is denied,
  1268.      * throw a SecurityException.
  1269.      *
  1270.      * Be very careful not to change the stack depth of this checkMemberAccess
  1271.      * call for security reasons reasons see
  1272.      * java.lang.SecurityManager.checkMemberAccess
  1273.      *
  1274.      * <p> Default policy: allow all clients access with normal Java access
  1275.      * control.
  1276.      */
  1277.     private void checkMemberAccess(int which, ClassLoader ccl) {
  1278.         SecurityManager s = System.getSecurityManager();
  1279.         if (s != null) {
  1280.             s.checkMemberAccess(this, which);
  1281.         ClassLoader cl = getClassLoader0();
  1282.             if ((ccl != null) && (ccl != cl) && 
  1283.                   ((cl == null) || !cl.isAncestor(ccl))) {
  1284.         String name = this.getName();
  1285.         int i = name.lastIndexOf('.');
  1286.         if (i != -1) {
  1287.             s.checkPackageAccess(name.substring(0, i));
  1288.         }
  1289.         }
  1290.     }
  1291.     }
  1292.  
  1293.     /**
  1294.      * Add a package name prefix if the name is not absolute Remove leading "/"
  1295.      * if name is absolute
  1296.      */
  1297.     private String resolveName(String name) {
  1298.         if (name == null) {
  1299.             return name;
  1300.         }
  1301.         if (!name.startsWith("/")) {
  1302.             Class c = this;
  1303.             while (c.isArray()) {
  1304.                 c = c.getComponentType();
  1305.             }
  1306.             String baseName = c.getName();
  1307.             int index = baseName.lastIndexOf('.');
  1308.             if (index != -1) {
  1309.                 name = baseName.substring(0, index).replace('.', '/')
  1310.                     +"/"+name;
  1311.             }
  1312.         } else {
  1313.             name = name.substring(1);
  1314.         }
  1315.         return name;
  1316.     }
  1317.  
  1318.  
  1319.     private native Field[] getFields0(int which);
  1320.     private native Method[] getMethods0(int which);
  1321.     private native Constructor[] getConstructors0(int which);
  1322.     private native Field getField0(String name, int which);
  1323.     private native Method getMethod0(String name, Class[] parameterTypes,
  1324.         int which);
  1325.     private native Constructor getConstructor0(Class[] parameterTypes,
  1326.         int which);
  1327.     private native Class[] getDeclaredClasses0();
  1328.  
  1329.  
  1330.     /** use serialVersionUID from JDK 1.1 for interoperability */
  1331.     private static final long serialVersionUID = 3206093459760846163L;
  1332.  
  1333.  
  1334.     /**
  1335.      * Class Class is special cased within the Serialization Stream Protocol. 
  1336.      *
  1337.      * A Class instance is written intially into an ObjectOutputStream in the 
  1338.      * following format:
  1339.      * <pre>
  1340.      *      <code>TC_CLASS</code> ClassDescriptor
  1341.      *      A ClassDescriptor is a special cased serialization of 
  1342.      *      a <code>java.io.ObjectStreamClass</code> instance. 
  1343.      * </pre>
  1344.      * A new handle is generated for the initial time the class descriptor
  1345.      * is written into the stream. Future references to the class descriptor
  1346.      * are written as references to the initial class descriptor instance.
  1347.      *
  1348.      * @see java.io.ObjectStreamClass
  1349.      */
  1350.     private static final ObjectStreamField[] serialPersistentFields = 
  1351.         ObjectStreamClass.NO_FIELDS;
  1352. }
  1353.